package com.gilight.base.controller;

import com.gilight.base.base.BusinessException;
import com.gilight.base.base.ParameterException;
import com.gilight.base.base.ResultEntity;
import com.gilight.base.common.Constant;
import com.gilight.base.common.RoleStatus;
import com.gilight.base.model.SysDataRule;
import com.gilight.base.model.SysPermission;
import com.gilight.base.model.SysUser;
import com.gilight.base.service.SysDataRuleService;
import com.gilight.base.service.SysPermissionService;
import com.gilight.base.service.SysRoleService;
import com.gilight.base.util.ShiroUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/web/sysPermission")
public class SysPermissionController {
    @Resource
    private SysRoleService sysRoleService;
    @Resource
    private SysPermissionService sysPermissionService;
    @Autowired
    private SysDataRuleService sysDataRuleService;
    @RequestMapping(value = "/all", method = RequestMethod.GET)
    public ResultEntity all(@RequestParam(required = false) String appKey) {
        ResultEntity resultEntity = new ResultEntity();
        List list = sysRoleService.allPermission(appKey);
        resultEntity.setData(list);
        return resultEntity;
    }
    @RequestMapping(value = "/getPermissionButton", method = RequestMethod.GET)
    public ResultEntity getPermissionButton(@RequestParam Long id) {
        ResultEntity resultEntity = new ResultEntity();
        List list = sysPermissionService.getPermissionButton(id);
        resultEntity.setData(list);
        return resultEntity;
    }
    @RequestMapping(value = "/getRolePermissionButton", method = RequestMethod.GET)
    public ResultEntity getRolePermissionButton(@RequestParam Long id,@RequestParam Long roleId) {
        ResultEntity resultEntity = new ResultEntity();
        List list = sysPermissionService.getRolePermissionButton(id,roleId);
        resultEntity.setData(list);
        return resultEntity;
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public ResultEntity add(@RequestBody SysPermission sysPermission) {
        ResultEntity resultEntity = new ResultEntity();
        sysPermissionService.add(sysPermission);
        return resultEntity;
    }
    @RequestMapping(value = "/addButton", method = RequestMethod.POST)
    @Transactional
    public ResultEntity addButton(@RequestBody SysPermission sysPermission) {
        sysPermission.setType(Constant.BUTTON);
        if(null==sysPermission.getRoute()||sysPermission.getRoute().trim().equals("")){
            throw new ParameterException("指定Key不能为空");
        }
        List<SysPermission> permissions = sysPermissionService.getPermissionButtonByRoute(sysPermission.getRoute());
        if(null!=permissions&&permissions.size()>0){
            throw new BusinessException("已经存在Key为"+sysPermission.getRoute()+"的按钮");
        }
        sysPermissionService.add(sysPermission);
        ResultEntity resultEntity = new ResultEntity();
        return resultEntity;
    }

    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public ResultEntity edit(@RequestBody SysPermission sysPermission) {
        ResultEntity resultEntity = new ResultEntity();
        sysPermissionService.edit(sysPermission);
        return resultEntity;
    }

    @RequestMapping(value = "/disable", method = RequestMethod.GET)
    public ResultEntity disable(@RequestParam Long id) {
        ResultEntity resultEntity = new ResultEntity();
        sysPermissionService.updateStatus(id, RoleStatus.DISABLE);
        return resultEntity;
    }

    /**
     * 根据菜单ID获取菜单数据规则
     * @param permissionId 菜单ID
     * @return
     */
    @RequestMapping(value = "/getPermDataRuleListByPermId", method = RequestMethod.GET)
    public ResultEntity getPermDataRuleListByPermId(@RequestParam Long permissionId) {
        ResultEntity resultEntity = new ResultEntity();
        List<SysDataRule> list = sysDataRuleService.selectByPermissionId(permissionId);
        resultEntity.setData(list);
        return resultEntity;
    }

    /**
     * 根据菜单ID获取当前登录人的角色分配的数据规则
     * @param permissionId 菜单ID
     * @return
     */
    @RequestMapping(value = "/getPermDataRuleListByPermIdAndUserId", method = RequestMethod.GET)
    public ResultEntity getPermDataRuleListByPermIdAndRoleId(@RequestParam Long permissionId) {
        ResultEntity resultEntity = new ResultEntity();
        // 获取当前登录人
        SysUser sysUser = ShiroUtil.getUser();
        if(sysUser != null){
            List<SysDataRule> list = sysDataRuleService.getPermDataRuleListByPermIdAndRoleId(permissionId,sysUser.getId());
            resultEntity.setData(list);
        }
        return resultEntity;
    }
    /**
     * 新增菜单数据规则
     * @param sysDataRule 菜单数据规则封装对象
     * @return
     */
    @RequestMapping(value = "/addPermissionDataRule", method = RequestMethod.POST)
    public ResultEntity addPermissionDataRule(@RequestBody SysDataRule sysDataRule) {
        ResultEntity resultEntity = new ResultEntity();
        sysDataRuleService.add(sysDataRule);
        return resultEntity;
    }

    /**
     * 修改菜单数据规则
     * @param sysDataRule 菜单数据规则封装对象
     * @return
     */
    @RequestMapping(value = "/editPermissionDataRule", method = { RequestMethod.PUT, RequestMethod.POST })
    public ResultEntity editPermissionDataRule(@RequestBody SysDataRule sysDataRule) {
        ResultEntity resultEntity = new ResultEntity();
        sysDataRuleService.edit(sysDataRule);
        return resultEntity;
    }

    /**
     * 根据数据规则主键ID删除数据规则信息
     * @param id 数据规则主键ID
     * @return
     */
    @RequestMapping(value = "/deletePermissionDataRule", method = RequestMethod.GET)
    public ResultEntity deletePermissionDataRule(@RequestParam Long id) {
        ResultEntity resultEntity = new ResultEntity();
        sysDataRuleService.delete(id);
        return resultEntity;
    }

}
